package com.fromdev.automation;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BloggerRedirectRulesBot {
	Properties prop = new Properties();	
	
	private WebDriver driver;
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();

	@Before
	public void setUp() throws Exception {
		driver = new FirefoxDriver();
		//driver = new ChromeDriver();		
		baseUrl = "https://www.blogger.com/";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	private void initConfig() throws IOException {
		Properties prop = new Properties();		  
		prop.load(this.getClass().getClassLoader().getResourceAsStream("blogger.properties"));		
	}
	@Test
	public void testDropDown() throws Exception {
		try {
		driver.get(baseUrl);
		System.out.println("Opening login page");
		driver.findElement(By.xpath("//*[@id='Email']")).sendKeys(
				prop.getProperty("blogger.user.name"));
		driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys(prop.getProperty("blogger.user.password"));
		Thread.sleep(2000);		
		driver.findElement(By.xpath("//*[@id='signIn']")).click();
		System.out.println("Hit login button");
		Thread.sleep(3000);		
		//Contact Blog
//		driver.get(baseUrl + "/blogger.g?blogID=2437074926760626775#searchsettings");
		String blogId = prop.getProperty("blogger.blog.id"); //"637499528247154499";
		driver.get(baseUrl + "/blogger.g?blogID=" +  blogId + "#searchsettings");		
		Thread.sleep(4000);		
		String [] errorUrlList =
	/*	{
				"/core-java-interview-questions"	,
				"/ankitgarg_er/collection-slides"	,
				"/news/2011"	,
				"/javase/tutorial/jndi/objects/serial.html"	,
				"/notes-java/background/13files_and_directories/20classpath.html"	,
				"/pub/papers/Simple_java_serialization_and_versioning.html"	,
				"/2008/07/struts-2-ajax-drop-down-example_644"	,
				"/ebook"	,
				"/s-computer__2bprogrammer__2binterview.aspx"	,
				"/best-wireframe-and-mockup-tool"	,
				"/2011/09/servlet-interview"	,
				"/l"	
		
						"/2011/07/opensource-web-application-firewall"	,
				"/interview_questions/java_collection_interview_questions.htm"	,
				"/test/25/Java-Interview-Questions"	,
				"/open-source-implementation.htm"	,
				"/2011/06/create-cdn-content-delivery-netwo"	,
				"/computer-programming/socket.aspx"	,
				"/="	,
				"/2011/08/11-most-in="	,
				"/p/fromdev-welcomes-guest-blogger.html="	,
				"/p/get-in-touch-there-is-nothing-bette="	,
				"/2012/10/Web-Site-A="	,
				"/2012/1="	,
				"/2012/08/Best-Open-Source-Web-Based-File-Explorer-Ap="	,
				"/2012/08/F="	,
				"/2011/08/11-most-influential-books-every.="	,
				"/2011/04/5-best-core-java-books-you-must-r="	,
				"/feeds/571429681978151="	,
				"/2012/="	,
				"/2010/12/interview-questions-hadoop-mapred="	,
				"/2012/09/Free-Open-Source="	,
				"/2012/09/Free-Open-Source-Java-Charting-Library.="	,
				"/2012/02/java-interview-question-answer.ht="	,
				"/2012/08/B="	,
				"/2012/02/java-interview-question-answe="	,
				"/2012/02/java-interv="	,
				"/2012/10/Web="	,
				"/shopping_cart_reviews.htm"	,
				"/2008/05/java-serialization-questions.html"	,
				"/jobs/senior-software-engineer-interview-questions-answers.html"	,
				"/~cpresser/java/index.html"	,
				"/best-mockup-design-tools-roundup"	,
				"/2012/10/learn"	,
				"/2011/04/top-20-core-java-interview-questions.html"	,
				"/questions/64856/senior-java-ee-interview-questions"	,
				"/class/cop5555fa09/"	,
				"/blog/guest-post/"	,
				"/articles/serialization/index.html"	,
				"/260347/java-concurrency-in-practice/goetz-brian-peierls-tim-bloch-joshua/"	,
				"/2010/03/how-to-monitor-process-on-linux-using.html"	,
				"/2012/04/top-5-open-source-amqp-advanced-message.html&amp;quot;&amp;gt;has"	,
				"/2012/02/how-to-show-minimize-and"	,
				"/why-there-are-two-date-classes--one-in-java-util-package-and-another-in-java-sql-"	,
				"/what-is-the-use-of-the-finally-block--is-finally-block-in-java-guaranteed-to-be-called--when-finally-block-is-not-called-"	,
				"/how-to-create-a-immutable-object-in-java--does-all-property-of-immutable-object-needs-to-be-final-"	,
				"/is-java-pass-by-reference-or-pass-by-value-"	,
				"/why-string-class-is-final-or-immutable-"	,
				"/what-is-immutable-object-in-java--can-you-change-values-of-a-immutable-object-"	,
				"/2011/12/10-excellent-opensource-vpn-solutio.."	,
				"/2010/04/15-best-wireframing-tools-for-designers.html"	,
				"/tutorial"	,
				"/2012/06/java-collections-interview-questions.html"	,
				"/blog/silvercart-an-overview/"	,
				"/software"	,
				"/2010/12/interview-questions"	,
		
				"/archives/10245"	,
				"/Hadoop_Summit/realtime-sentiment-analysis-app-using-hadoop"	,
				"/java-concurrency-in-practice"	,
				"/tools/13-javascript"	,
				"/terms/Sql_Interview_Questions_And_Answers_For_Freshers_Pdf_Free_Download"	,
				"/questions/5927363/java-resultset-setobject-vs-setstring-setdate-etc"	,
				"/open-source/content-managment-systems"	,
				"/computer-programming"	,
				"/wp-content/uploads/2006/09/core"	,
				"/web-dev/top-14-excellent-free-wireframe-tools/"	,
				"/2009/11/21-prototyping-mockup"	,
				"/InterviewWithAnswer.html"	,
				"/2009/07/firefox-40-mockup"	,
				"/staff/vero/languages/05/lectures/j"	,
				"/tips/15-crazy-useful-javascript-solutions-for-charts-and-graphs/"	,
				"/class/cs108/JavaBasicRefresher.pdf"	,
				"/post/7367118559"	,
				"/javascript/javascript-chart-library/"	,
				"/usabilitynews/92/shopping"	,
				"/discussions/thread.tss?thread_id=42283"	,
				"/jsp_"	,
				"/s-computer__2bprogrammer__2b"	,
				"/building"	,
				"/Java-Interview-Questions.aspx"	,
				"/j2ee-interview"	,
				"/ebook-pdf_c-programming"	,
				"/good-questions"	,
				"/55-Java-eBooks_1848454.html"	,
				"/rankings-of-best"	,
				"/xu/Link/java"	,
				"/~tcl/seg4210/coursenotes/Topic-G-JavaCollectionsFramework.ppt"	,
				"/kb/docs/php"	,
				};*/
		{		"/J2EE-Interview-Questions.aspx"	,
				"/courses/fall01/cps001/quiz/online-questions"	,
				"/downloads/recommendation"	,
				"/8301-13505_3-9935808-16.html"	,
				"/2008/02/07/shopping"	,
				"/2011/06/basic-java"	,
				"/vb/f44/t3663/"	,
				"/resources/ebs.php"	,
				"/releases/view/63640"	,
				"/home/cisc124/2011f/slides/collections"	,
				"/group/processing"	,
				"/articles/JavaComesOfAge.html"	,
				"/book/ppmetr/metaprogramming-ruby"	,
				"/display/CLOUDSYNC/Fedora"	,
				"/en-us/library/system.collections.hashtable"	,
				"/answers/viewqa/Framework/23609-datetimepicker-not-working-in-struts2.html"	,
				"/2011/11/collection-interview"	,
				"/2011/09/java"	,
				"/2012/10/learn-java"	,
				"/2011/07/java"	,
				"/2011/03/struts2"	,
				"/article/index.php?id=1338204805"	,
				"/watch?v=H23VUg22ByY"	,
				"/awesome-javascript"	,
				"/open"	,
				"/core"	,
				"/ecommerce"	,
				"/effective"	,
				"/2008/04/22/8-best"	,
				"/2010/11/upgrading-eclipse-to-helios-36.html"	,
				"/2010/05/21/free-open"	,
				"/2008/04/22/8-best-open"	,
				"/questions/12553577/how-to-take-screenshot-of-div-in-jsp"	,
				"/index.php/2012/01/16/top"	,
				"/jqchart/archive/2011/07/26/real-time-charts"	,
				"/blog/bid/144653/6-of-the-Best-Open-Source-Shopping-Cart-Software-Features-...www.fromdev.com/blog/bid/144653/6-of-the-Best-Open-Source-Shopping-Cart-Software-Features-You-Need"	,
				"/Java/Article/32790"	,
				"/company/news/events/"	,
				"/archives/2009/03/mac"	,
				"/2012/05/15-awesome-open-source-java"	,
				"/2011/12/10-excellent-opensource"	,
				"/2011/04/top-20-core"	,
				"/2012/07/best-open"	,
				"/html/open-source"	,
				"/2008/12/debugging-java-on-unixlinux-my-favorite"	,
				"/2012/06/java-collections-uml-class-diagrams.html"	,
				"/book/-/9781849515801/1dot-setting-up-your-development-environment/id286697...www.fromdev.com/book/-/9781849515801/1dot-setting-up-your-development-environment/id286697446"	,
				"/display/STUDENTDOC/2.3.1"	,
				"/2012/08/Things-H.."	,
				"/2012/04/8-best-s.."	,
				"/p/fromdev-welcomes-guest"	,
				"/2011/04/5-best-core"	,
				"/chapter-10-transaction-management-and-concurrency"	,
				"/2008/12/javalangveri.."	,
				"/2010/08/best"	,
				"/questions/9230804/best-free-php"	,
				"/interview"	,
				"/2008/12/debugging-java-o.."	,
				"/tutorials/make-stylish-preloader-svg"	,
				"/blog/webmaster/new-beta-version-of-adobe-muse-wireframing-tool"	,
				"/t/529157/tomcat"	,
				"/questions/2251083/php"	,
				"/pub/a/on"	,
				"/java/docs/tutorial"	,
				"/infocenter/db2luw/v8/topic/com.ibm.db2.udb.doc/ad/rjvjcdif.htm"	,
				"/blog/15-desktop-wireframing-mockup-prototyping-tools"	,
				"/02/programming/mockflow-free"	,
				"/2012/09/free-open"	,
				"/2011/.../10-best-free"	,
				"/feeds/posts/default?alt"	,
				"/%E2%80%9D]FromDev:"	,
				"/feeds/5714296819781513221/comments/defau="	,
				"/feeds/posts/default?alt=3Drss"	,
				"/p/contact.html"	
		};
		/*
		 * after 10 redirects are added every redired input xpath is at tr 10
		 * This may change after another redesign of blogger.
		 */
		int start = 10;
		try {
			int existingRedirCount = Integer.parseInt(prop.getProperty("blogger.existing.redirect.count"));
			if (existingRedirCount < 10) {
				start = existingRedirCount;
			}
		}catch(Exception e) {}
		
		for(int i =0;i<errorUrlList.length;i++) {
			try {
			//Loop starts here
			driver.findElement(By.xpath("/html/body/div[3]/div[4]/div/div/div[2]/div[2]/div[2]/div[6]/div/div[2]/table/tbody/tr[2]/td[2]/div/span/a")).click(); 
			Thread.sleep(TimeUtil.getTypingTimeSeconds());		
			driver.findElement(By.xpath("/html/body/div[3]/div[4]/div/div/div[2]/div[2]/div[2]/div[6]/div/div[2]/table/tbody/tr[2]/td[2]/div/div/div[1]/div[1]/div[1]/button[1]")).click(); 
			Thread.sleep(TimeUtil.getTypingTimeSeconds());				
			String errorUrlInputXPath =    "/html/body/div[3]/div[4]/div/div/div[2]/div[2]/div[2]/div[6]/div/div[2]/table/tbody/tr[2]/td[2]/div/div/div[1]/div[2]/table/tbody[1]/tr[" + start + "]/td[2]/div/span[2]/span/input";
			driver.findElement(By.xpath(errorUrlInputXPath)).sendKeys(errorUrlList[i]);
			Thread.sleep(TimeUtil.getTypingTimeSeconds());		
			String redirectUrlInputXPath = "/html/body/div[3]/div[4]/div/div/div[2]/div[2]/div[2]/div[6]/div/div[2]/table/tbody/tr[2]/td[2]/div/div/div[1]/div[2]/table/tbody[1]/tr[" + start + "]/td[2]/div/span[5]/span/input";
			driver.findElement(By.xpath(redirectUrlInputXPath)).sendKeys("/");
			Thread.sleep(TimeUtil.getTypingTimeSeconds());	
			String permanetCheckboxXPath = "/html/body/div[3]/div[4]/div/div/div[2]/div[2]/div[2]/div[6]/div/div[2]/table/tbody/tr[2]/td[2]/div/div/div[1]/div[2]/table/tbody[1]/tr[" + start + "]/td[2]/div/span[8]/span/input";
			if(!driver.findElement(By.xpath(permanetCheckboxXPath)).isSelected()) {
				driver.findElement(By.xpath(permanetCheckboxXPath)).click(); 
			}
			Thread.sleep(TimeUtil.getTypingTimeSeconds());	
			String localSaveButtonXPath = "/html/body/div[3]/div[4]/div/div/div[2]/div[2]/div[2]/div[6]/div/div[2]/table/tbody/tr[2]/td[2]/div/div/div[1]/div[2]/table/tbody[1]/tr[" + start + "]/td[2]/div/span[10]/span/div/a[1]";
			driver.findElement(By.xpath(localSaveButtonXPath)).click(); 
			Thread.sleep(TimeUtil.getTypingTimeSeconds());				
			driver.findElement(By.xpath("/html/body/div[3]/div[4]/div/div/div[2]/div[2]/div[2]/div[6]/div/div[2]/table/tbody/tr[2]/td[2]/div/div/div[2]/button[1]")).click(); 
			start++;
			if(start >10) {
				start = 10;
			}
			Thread.sleep(TimeUtil.getTypingTimeSeconds()*15);
			}catch(Exception e) {
				System.out.println("Last Failed URL " + errorUrlList[i]);
				throw e;
			
			}
		}
		System.out.println("Successfully Completed this round");
		// *[@id='signIn']

		/*
		 * driver.get(baseUrl +
		 * "/rma.htm?_flowExecutionKey=_c81136274-639B-9405-F8E2-380B87D14744_k95A78D42-64A1-417D-A116-297BA18D40A1"
		 * ); driver.findElement(By.id("manual_ps3_slim")).click();
		 * driver.findElement(By.id("terms")).click();
		 * driver.findElement(By.cssSelector("img")).click(); new
		 * Select(driver.findElement
		 * (By.id("modelNum"))).selectByVisibleText("CECH-2501B (PS3 320GB)");
		 * driver.findElement(By.id("txtSerial")).clear();
		 * driver.findElement(By.id("txtSerial")).sendKeys("cd334243223");
		 * driver.findElement(By.id("proofOfPurchase1")).click();
		 * driver.findElement(By.cssSelector("a.continue > span")).click();
		 */
		}catch(Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
}
